<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="css/aui.css">
    <script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="js/init.js"></script>
    <title>密保信息</title>
</head>
<body>
<header>
    <a href="javascript:history.back(-1)" class="f1 head-back"></a>
    <div class="f8">密保信息</div>
    <div class="f1"></div>
</header>
<section class="bg-gray">
    <div class="pad-3">
        <ul>
            <li class="re">
                <b class="color-gray">密保问题</b>
                <div id="ques" class="bg-white pad-1-2 border-radius-1 border-gray mar-t-1 mar-b-3">
                    <span id="questxt" data-id="1">您的出生地？</span>
                    <span class="arrow3 fr mar-t-1"></span>
                </div>
                <ul id="quesbox" class="ab col-10 bg-white pad-1-2 border-radius-1 line-h-2 border-gray none" >
                    <li data-id="1">您的出生地？</li>
                    <li data-id="2">您的生日？</li>
                    <li data-id="3">您的老家？</li>
                </ul>
            </li>
            <li>
                <b class="color-gray">密保答案</b>
                <div class="bg-white pad-1-2 border-radius-1 border-gray mar-t-1 mar-b-3">

                    <input id="answer" class="col-10" type="text" placeholder="请输入密码答案">
                </div>
            </li>
            <li>
                <b class="color-gray">二级密码</b>
                <div class="bg-white pad-1-2 border-radius-1 border-gray mar-t-1 mar-b-3">

                    <input id="passward" class="col-10" type="password" placeholder="请输入二级密码">
                </div>
            </li>

            <li class="col-3 bg-blue border-radius-1 color-white center line-h-2 mar-l-5" onclick="modify()">修改</li>
        </ul>
    </div>
</section>

<script src="js/foot.js"></script>
<script>
    // 选择密保问题
    var type = 1;
    $("#ques").click(function () {
        $("#quesbox").show();
    });
    $("#quesbox li").click(function () {
        var text = $(this).text();
        $("#questxt").text(text);  //密保问题赋值
        $("#quesbox").hide();
        // $("#questxt").attr('data-id',$(this).attr('data-id'));
        //设置密保问题类型
        type = $(this).attr('data-id');
        // 设置密保答案
        type == 1?$("#answer").val(userinfo.encrypted1):type == 2?$("#answer").val(userinfo.encrypted2):type == 3?$("#answer").val(userinfo.encrypted3):'';
    });


    // 获取密保
    var userinfo;
    var data;
    new AjaxRequest({
        type: 'post',
        dataType: 'json',
        url: hostUrl + 'index/User/userinfo',
        data: data,
        callback: res => {
            if(res.status == 1){
                userinfo = res.data;
                type == 1?$("#answer").val(userinfo.encrypted1):type == 2?$("#answer").val(userinfo.encrypted2):type == 3?$("#answer").val(userinfo.encrypted3):'';
            }

        }
    });

    // 修改密保
    function modify() {
        var answer = $("#answer").val();
        var pwd = $("#passward").val();
        if(!answer || !pwd){
            toast('请填写完整信息');
            return;
        }

        var data = {};
        data.userid = localStorage.user_id;
        data.token = localStorage.token;
        data.password_2 = pwd;
        type == 1?data.encrypted1=answer:type == 2?data.encrypted2=answer:type == 3?data.encrypted3=answer:'';
        new AjaxRequest({
            type: 'post',
            dataType: 'json',
            url: hostUrl + 'index/Mycenter/setencrypted',
            data: data,
            callback: res => {
                toast(res.msg);
                if(res.status == 1){
                    setTimeout(function () {
                        location.href = 'my.html';
                    },2000)
                }

            }
        });
    }
</script>

</body>
</html>